package org.xawl.news.dao.impl.role;

import java.util.List;

import org.xawl.news.dao.role.RoleDao;
import org.xawl.news.entity.role.Role;
import org.xawl.news.util.DBUtil;

public class RoleDaoImpl implements RoleDao {

	@Override
	public List<Role> findList() {
		String sql = "select * from t_role";
		List<Role> roles = DBUtil.getList(Role.class, sql);
		return roles;
	}

	@Override
	public boolean save(Role role) {
		String sql = "insert into t_role(name, code, description, create_time, update_time) values(?,?,?,?,?)";
		Object[] params = {role.getName(), role.getCode(), role.getDescription(), role.getCreateTime(), role.getUpdateTime()};
		boolean flag = DBUtil.executeUpdate(sql, params);
		return flag;
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from t_role where id=?";
		boolean flag = DBUtil.executeUpdate(sql, id);
		return flag;
	}

	@Override
	public boolean update(Role role) {
		String sql = "update t_role set name=?, code=?, description=?, create_time=?, update_time=? where id=?";
		Object[] params = {role.getName(), role.getCode(), role.getDescription(), role.getCreateTime(), role.getUpdateTime(), role.getId()};
		boolean flag = DBUtil.executeUpdate(sql, params);
		return flag;
	}

	@Override
	public int findCount() {
		String sql = "select count(*) from t_role";
		int count = DBUtil.getCount(sql);
		return count;
	}

	@Override
	public Role findById(int id) {
		String sql = "select * from t_role where id=?";
		Role role = DBUtil.getObject(Role.class, sql, id);
		return role;
	}

}
